﻿using System;
using System.Reflection;
using Autodesk.Revit.DB;

namespace YJKRevitCode.TransformUtility
{
	public static class TransformUtilityMethod
	{
		public static XYZ TransformPoint(XYZ point, Transform transform)
		{
			if (transform == null)
			{
				return point;
			}
			double x = point.X;
			double y = point.Y;
			double z = point.Z;
			XYZ xyz = transform.get_Basis(0);
			XYZ xyz2 = transform.get_Basis(1);
			XYZ xyz3 = transform.get_Basis(2);
			XYZ origin = transform.Origin;
			double num = x * xyz.X + y * xyz2.X + z * xyz3.X + origin.X;
			double num2 = x * xyz.Y + y * xyz2.Y + z * xyz3.Y + origin.Y;
			double num3 = x * xyz.Z + y * xyz2.Z + z * xyz3.Z + origin.Z;
			return new XYZ(num, num2, num3);
		}

		public static Solid TransformSolid(Solid input, Transform inputTransform)
		{
			if (inputTransform == null)
			{
				return input;
			}
			Type typeFromHandle = typeof(SolidUtils);
			Solid result = null;
			try
			{
				result = (Solid)typeFromHandle.InvokeMember("CreateTransformed", BindingFlags.InvokeMethod, null, null, new object[]
				{
					input,
					inputTransform
				});
			}
			catch (Exception)
			{
				result = input;
			}
			return result;
		}

		public static Transform CalculateTransformFromPlanarFaceToXoY(PlanarFace inputFace)
		{
			Transform transform = Transform.CreateTranslation(XYZ.Zero);
			transform.Origin = inputFace.Origin;
			transform.BasisX = inputFace.XVector;
			transform.BasisY = inputFace.YVector;
			transform.BasisZ = -inputFace.FaceNormal;
			return transform.Inverse;
		}
	}
}
